﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using SW=Stauffware.Common.Core;
using Stauffware.Common.IO;

namespace UnitTests.IO
{
	public class FileSysPath_Tests
	{
		//-----------------------------------------------------------------------------
		// In these testcases, the "#" character is replaced with the current working directory path.

		[TestCase(".", "#")]
		[TestCase(".", "#")]
		[TestCase("C:\\", "C:\\")]
		[TestCase("#", "#")]
		[TestCase("A:\\non\\existent\\directory\\*.*", "A:\\non\\existent\\directory\\*.*")]
		[TestCase("foo\\*.bak", "#\\foo\\*.bak")]

		public static void GetFullPath (string input, string expectedOutput)
		{
			input = input.Replace("#", Environment.CurrentDirectory);
			expectedOutput = expectedOutput.Replace("#", Environment.CurrentDirectory);

			string actualOutput = FileSystemPath.GetFullPath(input);
			AssertStringsMatch(expectedOutput, actualOutput);
		}

		//-----------------------------------------------------------------------------

		[TestCase("x", "x")]
		[TestCase("c:\\", "c:\\")]
		[TestCase("/foo", "\\foo")]
		[TestCase("a/b/c/..", "a\\b")]
		[TestCase("a/b/c/../../x", "a\\x")]
		public static void NormalizePath (string input, string expectedOutput)
		{
			string actual = FileSystemPath.NormalizePath(input, '\\');
			AssertStringsMatch(expectedOutput, actual);
		}

		//-----------------------------------------------------------------------------

		[TestCase("%windir%", "C:\\WINDOWS")]
		[TestCase("%WiNdIr%", "C:\\WINDOWS")]
		[TestCase("%windir%", "C:\\WINDOWS")]
		[TestCase("<%windir%", "<C:\\WINDOWS")]
		[TestCase("%windir%>", "C:\\WINDOWS>")]
		[TestCase("<%windir%>", "<C:\\WINDOWS>")]
		[TestCase("%ComSpec%", "C:\\WINDOWS\\system32\\cmd.exe")]
		[TestCase("%ComSpec%%windir%", "C:\\WINDOWS\\system32\\cmd.exeC:\\WINDOWS")]
		[TestCase("<%%>", "<%>")]

		//[TestCase("", "")]
		//[TestCase("%model%", "E6410")]
		//[TestCase("a%model%", "aE6410")]
		//[TestCase("%model%b", "E6410b")]
		//[TestCase("a%model%b", "aE6410b")]

		public static void ResolveEnvVar (string input, string expectedOutput)
		{
			string actualOutput = FileSystemPath.ResolveEnvironmentVariables(input);
			AssertStringsMatch(expectedOutput, actualOutput);
		}

		[TestCase("%System%", "C:\\WINDOWS\\system32")]
		[TestCase("<%System%>", "<C:\\WINDOWS\\system32>")]
		public static void ResolveFolder (string input, string expectedOutput)
		{
			string actualOutput = FileSystemPath.ResolveSpecialFolders(input);
			AssertStringsMatch(expectedOutput, actualOutput);
		}

		//-----------------------------------------------------------------------------
		// This is just to provide reference data for debugging.

		[TestCase]
		public static void DumpEnvVars_NotATest ( )
		{
			IDictionary dict = Environment.GetEnvironmentVariables();
			foreach (string key in dict.Keys)
			{
				string value = (string) dict[key];
				if (key.ToLower() == "path")
					value = value.Replace(";", ";\r\n    ");
				System.Console.WriteLine(key + " = " + value);
			}
		}

		//-----------------------------------------------------------------------------

		public static void AssertStringsMatch (string expected, string actual)
		{
			if (!string.Equals(expected, actual, StringComparison.OrdinalIgnoreCase))
				Assert.Fail("Expected: " + SW.ErrorUtils.ValueToString(expected) +
						"\r\n But was: " + SW.ErrorUtils.ValueToString(actual));
		}


		//-----------------------------------------------------------------------------
	}
}
